{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生产者 p2 休眠了 3 秒\n",
      "生产者p2 生产了数据29\n",
      "消费者 c2 休眠了 2 秒生产者 p3 休眠了 2 秒\n",
      "生产者p3 生产了数据0\n",
      "\n",
      "消费者c2 消费了数据0\n",
      "生产者 p2 休眠了 2 秒\n",
      "生产者p2 生产了数据46\n",
      "消费者 c1 休眠了 5 秒\n",
      "消费者c1 消费了数据46\n",
      "消费者 c2 休眠了 3 秒生产者 p3 休眠了 3 秒\n",
      "生产者p3 生产了数据6\n",
      "\n",
      "消费者c2 消费了数据6\n",
      "生产者 p2 休眠了 3 秒\n",
      "生产者p2 生产了数据12\n",
      "消费者 c2 休眠了 1 秒\n",
      "消费者c2 消费了数据12\n",
      "生产者 p2 休眠了 1 秒\n",
      "生产者p2 生产了数据74\n",
      "生产者 p3 休眠了 2 秒\n",
      "生产者p3 生产了数据43\n",
      "消费者 c1 休眠了 5 秒\n",
      "消费者c1 消费了数据74\n",
      "生产者 p2 休眠了 2 秒\n",
      "生产者p2 生产了数据7\n",
      "消费者 c1 休眠了 1 秒\n",
      "消费者c1 消费了数据43\n",
      "消费者 c1 休眠了 1 秒\n",
      "消费者c1 消费了数据7\n",
      "消费者 c2 休眠了 4 秒生产者 p3 休眠了 3 秒\n",
      "\n",
      "生产者p3 生产了数据55\n",
      "消费者c2 消费了数据55\n",
      "消费者 c2 休眠了 1 秒\n",
      "生产者 p2 休眠了 3 秒\n",
      "生产者p2 生产了数据36消费者c2 消费了数据36\n",
      "\n",
      "生产者 p3 休眠了 2 秒\n",
      "生产者p3 生产了数据33\n",
      "消费者 c1 休眠了 3 秒\n",
      "消费者c1 消费了数据33\n",
      "生产者 p2 休眠了 2 秒\n",
      "生产者p2 生产了数据29\n",
      "消费者 c2 休眠了 3 秒\n",
      "消费者c2 消费了数据29\n",
      "生产者 p3 休眠了 3 秒\n",
      "生产者p3 生产了数据50\n",
      "消费者 c1 休眠了 4 秒生产者 p2 休眠了 3 秒\n",
      "生产者p2 生产了数据0\n",
      "\n",
      "消费者c1 消费了数据50\n",
      "生产者 p3 休眠了 2 秒\n",
      "生产者p3 生产了数据62\n",
      "生产者 p2 休眠了 1 秒\n",
      "生产者p2 生产了数据99\n",
      "生产者 p3 休眠了 1 秒\n",
      "生产者p3 生产了数据80\n",
      "消费者 c2 休眠了 4 秒\n",
      "消费者c2 消费了数据0\n",
      "生产者 p2 休眠了 1 秒\n",
      "生产者p2 生产了数据32\n",
      "消费者 c1 休眠了 3 秒\n",
      "消费者c1 消费了数据62\n"
     ]
    }
   ],
   "source": [
    "from threading import Thread,current_thread\n",
    "from queue import Queue\n",
    "import time\n",
    "import random\n",
    "\n",
    "queue = Queue(5)\n",
    "\n",
    "class ProducerThread(Thread):\n",
    "    def run(self):\n",
    "        name = current_thread().getName()\n",
    "        nums = range(100)\n",
    "        global queue\n",
    "        while True:\n",
    "            num = random.choice(nums)\n",
    "            queue.put(num)\n",
    "            print('生产者{} 生产了数据{}'.format(name,num))\n",
    "            t = random.randint(1,3)\n",
    "            time.sleep(t)\n",
    "            print('生产者 {} 休眠了 {} 秒'.format(name,t))\n",
    "\n",
    "class ConsumerThead(Thread):\n",
    "    def run(self):\n",
    "        name = current_thread().getName()\n",
    "        global queue\n",
    "        while True:\n",
    "            num = queue.get()\n",
    "            queue.task_done()\n",
    "            print('消费者{} 消费了数据{}'.format(name,num))\n",
    "            t = random.randint(1,5)\n",
    "            time.sleep(t)\n",
    "            print('消费者 {} 休眠了 {} 秒'.format(name,t))\n",
    "\n",
    "\n",
    "# p1 = ProducerThread(name='p1')\n",
    "# p1.start()\n",
    "# p2 = ProducerThread(name='p2')\n",
    "# p2.start()\n",
    "# p3 = ProducerThread(name='p3')\n",
    "# p3.start()\n",
    "\n",
    "# c1 = ConsumerThead(name='c1')\n",
    "# c1.start()\n",
    "# c2 = ConsumerThead(name='c2')\n",
    "# c2.start()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "f0831691c6bb6c483f09af1e7987fd76373eadc68cb706017f41b090bfe4d138"
  },
  "kernelspec": {
   "display_name": "Python 3.8.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
